package ikelib;
import java.awt.Component;
import java.awt.TextField;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.*;
import java.io.File;
import java.io.FilenameFilter;
import java.util.Iterator;
import java.util.List;
import java.util.TreeSet;
import java.util.Vector;

import javax.swing.JComboBox;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;



public class FileDropTarget extends DropTarget {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void drop(DropTargetDropEvent d){
		Transferable t = d.getTransferable();
		if(t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)){
			d.acceptDrop(DnDConstants.ACTION_REFERENCE);
			try{
				List<File> list = (List<File>)t.getTransferData(DataFlavor.javaFileListFlavor);
				String fname=new String("");
				for(int i=0;i<list.size();i++){
					File f = list.get(i);
					if(i!=0){
						fname += ";";
					}
					fname += f.getPath();
				}
				Component c = this.getComponent();
				if(c instanceof JTextField){
					((JTextField)c).setText(fname);
				}
				else if(c instanceof JComboBox){
					JComboBox cb = (JComboBox)c;
					cb.insertItemAt(fname,0);
					cb.getModel().setSelectedItem(fname);
					for(int i=1;i<cb.getItemCount();i++){
						String str = (String)cb.getItemAt(i);
						if(fname.compareToIgnoreCase(str)==0){
							cb.removeItemAt(i);
						}
					}
					cb.getModel().setSelectedItem(fname);
				}
				else if(c instanceof JTable){
					JTable tbl = (JTable)c;
					DefaultTableModel mdl = (DefaultTableModel)tbl.getModel();
					int maxrow = mdl.getRowCount();
					int start=0;
					for(;start<maxrow;start++){
						String no = (String)mdl.getValueAt(start,0);
						if(no==null) break;
						if(no.isEmpty()){
							mdl.removeRow(start);
							break;
						}
					}
					// ソートするために
					TreeSet<String> set = new TreeSet<String>();
					for(int i=0;i<list.size();i++){
						File f = list.get(i);
						String fn = f.getAbsolutePath();
						set.add(fn);
					}
					String[] str = new String[set.size()];
					set.toArray(str);
					set = Dir2Files(str);
					Iterator<String> ite = set.iterator();
					
					int colno	 = tbl.getColumn("No").getModelIndex();
					int colFile = tbl.getColumn("Input File").getModelIndex();
					int colUse = tbl.getColumn("use").getModelIndex();
					
					while(ite.hasNext()){
						String fn=ite.next();
						mdl.addRow(new Vector<String>());
						mdl.setValueAt(String.format("%d",start),start,colno);
						mdl.setValueAt(fn,start, colFile);
						mdl.setValueAt(true, start, colUse);
						start++;
					}
				}
			}catch(Exception e){
				throw new RuntimeException(e);
			}
		}
	}
	
	//--------------------------------------------------------------------------------
	private static int findFiles(String fname,TreeSet<String> ts){
		int count = 0;
		File f = new File(fname);
		if(f.isFile()){
			ts.add(f.getAbsolutePath());
			count++;
			//System.out.println("Dropped file:"+f.getAbsolutePath());
		}
		else if(f.isDirectory()){
			String[] ret = f.list(new FilenameFilter(){
				public boolean accept(File dir, String name){
					return true;
				}
			});
			for(int j=0;j<ret.length;j++){
				findFiles(fname + new String(f.separator) + ret[j],ts);
			}
		}
		return count;
	}
	// フォルダを含むファイルを個別のファイル名に変換する
	public static TreeSet<String> Dir2Files(String[] dir){
		TreeSet<String> ts=null;
		ts = new TreeSet<String>();
		for(int i=0;i<dir.length;i++){
			findFiles(dir[i],ts);
		}
		return ts;
	}
	// ；で区切られたファイルのリストを返す。ディレクトリがあればその下を再帰的に探索する。
	public static TreeSet<String> GetFiles(String filestring){
		String[] files = filestring.split(";",100);
		return Dir2Files(files);
	}
		

	
	
	
}
